<?php
class TvbDarwinDistributor extends BaseDistributor {
	const DARWIN_BASE_RTSP = 'rtsp://m1.vdo.tvb.com';
	const DARWIN_BASE_HTTP = 'http://m1.vdo.tvb.com';
	
	public function getDestinationId() {
		return 3;
	}

	public function distribute(array $data) {
		return TRUE;
	}
	
	public function getVideo($export_id) {
		$rf = VideoInfrastructure::GetReadableRecordFactory();
		
		$found = $rf->find(
			'Distribution',
			'export_id=? AND destination_id=?',
			array($export_id, $this->getDestinationId())
		);
		if (!$found) {
			return -2;	// Export video not found
		}
		
		$distribution_entry = array_shift($found);
		if ($distribution_entry->status != 1) {
			return $distribution_entry->status; // Video is not ready
		}
		
		$export_entry = $rf->findInstance('Export', $export_id);
		if (!$export_entry) {
			return -3;
		}
		
		$other_info = json_decode($distribution_entry->other_info, TRUE);
		$file = $other_info['darwin_video_source'];
		$file_path = str_replace('//', '/', $other_info['darwin_video_source']);
		
		return array(
			'file' => "{$file_path}",
			'rtsp_base' => self::DARWIN_BASE_RTSP,
			'rtsp' => self::DARWIN_BASE_RTSP . "/{$file_path}",
			'http_base' => self::DARWIN_BASE_HTTP,
			'http'	=> self::DARWIN_BASE_HTTP . "/{$file_path}",
		);
	}
	
	public function getVideoListBySourceId($source_id) {
	}
}

$distributor = new TvbDarwinDistributor();
$ret = DistributorFactory::getInstance()->register($distributor->getDestinationId(), $distributor);
?>
